//
//  MYGSettingCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/11.
//

import UIKit

class MYGSettingCell: UITableViewCell {
    // MARK: - Property
    var model: MYGSettingCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            nameLabel.text = model.name
            switch model.type {
            case .enterText(let string):
                switchButton.isHidden = true
                hintLabel.isHidden = false
                rightImageView.isHidden = false
                hintLabel.text = string
            case .text(let string):
                switchButton.isHidden = true
                hintLabel.isHidden = false
                rightImageView.isHidden = true
                hintLabel.text = string
            case .button(let bool):
                switchButton.isHidden = false
                hintLabel.isHidden = true
                rightImageView.isHidden = true
                switchButton.isSelected = bool
            }
            
            lineView.isHidden = !model.isBottom
            
            var corner: CACornerMask = []
            
            if model.isTop {
                corner.insert(.layerMinXMinYCorner)
                corner.insert(.layerMaxXMinYCorner)
            }
            
            if model.isBottom {
                corner.insert(.layerMaxXMinYCorner)
                corner.insert(.layerMaxXMaxYCorner)
            }
            
            mainView.layer.maskedCorners = corner
        }
    }
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .rgba(249, 249, 249, 1)
        
        contentView.addSubview(mainView)
        mainView.addSubview(nameLabel)
        mainView.addSubview(stackView)
        stackView.addArrangedSubviews([hintLabel, switchButton, rightImageView])
        mainView.addSubview(lineView)
        
        mainView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.bottom.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        
        stackView.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(20)
            make.centerY.equalToSuperview()
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(23)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    // MARK: - lazy load
    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 5
        return view
    }()
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(51, 51, 51, 1)
        return label
    }()
    
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.spacing = 6
        stackV.alignment = .center
        return stackV
    }()
    
    private lazy var hintLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(104, 104, 104, 1)
        return label
    }()
    
    private lazy var switchButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Setting/switch_sel"), for: .selected)
        btn.setImage(UIImage(named: "Setting/switch"), for: .normal)
        return btn
    }()
    
    private lazy var rightImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "Common/right"))
        return imgV
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
}
